package com.probiz.estore.content.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.common.model.content.ContentRelatedType;
import com.probiz.estore.common.model.content.RelatedContent;
import com.probiz.estore.common.model.sales.RecommendedType;
import com.probiz.estore.content.dao.RelatedContentDao;
import com.probiz.estore.content.model.ContentConstants;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;

/**
 * Dao implementation for RelatedContent.
*/
public class RelatedContentDaoImpl extends HibernateGenericDaoImpl<RelatedContent> implements RelatedContentDao {

	public List<RelatedContent> getRelatedContentsByRecommendedTypeIdBySourceId(
			final int contentRelatedTypeId, final  Integer sourceKind, final  Integer sourceId,
			final  int firstResult, final  int maxResults) {
		List list;
		list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(200);
				sb.append("from RelatedContent  where ");
				sb.append(" contentRelateType.contentRelateTypeId=" + contentRelatedTypeId);
				
				switch (sourceKind){
					case ContentConstants.RELATED_SOURCE_KIND_CONTENT: 
						sb.append(" and sourceContent.contentId=" + sourceId);break;
					case ContentConstants.RELATED_SOURCE_KIND_CONTENTCATEGORY: 
						sb.append(" and sourceCategory.categoryId=" + sourceId);break;
					default: break;
					
				}
				sb.append(" order by relatedContentId desc ");
				Query query = session.createQuery(sb.toString());
				query.setFirstResult(firstResult);
				if (maxResults != -1)
					query.setMaxResults(maxResults);
				List<RelatedContent> rows = (List<RelatedContent>) query
						.list();
				
				return rows;
			}

		});
		return list;
	}
	
	public RelatedContent getRelatedContent(Integer recommendedTypeId,
			int sourceKind, Integer sourceId, Integer contentId) {
		StringBuffer sb = new StringBuffer(200);
		sb.append("from RelatedContent where ");
		sb.append(" contentRelatedType.contentRelatedTypeId=" + recommendedTypeId);
		switch (sourceKind){
			case ContentConstants.RELATED_SOURCE_KIND_CONTENT: 
				sb.append(" and sourceContent.contentId=" + sourceId);break;
			case ContentConstants.RELATED_SOURCE_KIND_CONTENTCATEGORY: 
				sb.append(" and sourceCategory.categoryId=" + sourceId);break;
			default: break;
			
		}
		sb.append(" and content.contentId=" + contentId);
		sb.append(" order by relatedContentId desc ");

		List list = getHibernateTemplate().find(sb.toString());
		if (list.size() > 0) {
			return (RelatedContent) list.get(0);
		} else {
			return null;
		}
	}
	
	public List<Content> getContentsByRelatedTypeNameBySourceId(
			final String contentRelatedTypeName,final int sourceKind, final Integer sourceId,
			final int firstResult,final  int maxResults) {
		List list;
		list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(200);
				sb.append("select distinct(rc.content) from RelatedContent rc  where ");
				sb.append(" rc.contentRelatedType.typeName='"+ contentRelatedTypeName + "'");
				switch (sourceKind){
					case ContentConstants.RELATED_SOURCE_KIND_CONTENT: 
						sb.append(" and rc.sourceContent.contentId=" + sourceId);break;
					case ContentConstants.RELATED_SOURCE_KIND_CONTENTCATEGORY: 
						sb.append(" and rc.sourceCategory.categoryId=" + sourceId);break;
					default: break;
					
				}
				sb.append(" order by rc.sortOrder asc ");
				Query query = session.createQuery(sb.toString());
				query.setFirstResult(firstResult);
				if (maxResults != -1)
					query.setMaxResults(maxResults);
				List<Content> rows = (List<Content>) query.list();
				 
				return rows;
			}

		});
		return list;
	}
	public List<Content> getContentsByRelatedTypeIdBySourceId(
			final Integer contentRelatedTypeId, final int sourceKind, final Integer sourceId,
			final int firstResult, final int maxResults) {
		ContentRelatedType contentRelatedType = (ContentRelatedType) getHibernateTemplate().get(RecommendedType.class, contentRelatedTypeId);
		if (null == contentRelatedType)		return new ArrayList<Content>();
		return this.getContentsByRelatedTypeNameBySourceId(contentRelatedType.getTypeName(), sourceKind, sourceId,firstResult, maxResults);
	}


	

	

}
